package com.example.demo.controller;

import com.example.demo.entity.BaseResponse;
import com.example.demo.entity.UpdateLog;
import com.example.demo.mapper.UpdateLogMapper;
import com.example.demo.service.StuAnswerService;
import com.example.demo.service.UpdateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

@RequestMapping("/stuanswer")
@CrossOrigin
@RestController
public class StuAnswerController {
    @Autowired
    private StuAnswerService stuAnswerService;
    @Autowired
    private UpdateLogService updateLogService;
    /*添加作答结果*/
    @PostMapping(value = "/addStuAnswer")
    private BaseResponse<Map<String,Object>> addStuAnswer(@RequestParam(value = "studentId") String studentId,
                                            @RequestParam(value = "exerciseId") int exerciseId,
                                            @RequestParam(value = "questionId") int questionId,
                                            @RequestParam(value = "answer") String answer,
                                            @RequestParam(value = "isRight") boolean isRight,
                                            @RequestParam(value = "idea") String idea,
                                            @RequestParam(value = "score") int score)throws ParseException{
       Map<String,Object> result=stuAnswerService.addStuAnswer(studentId, exerciseId, questionId, answer, isRight, idea, score);
       System.out.println("-------------------------------------");
       System.out.println(studentId+exerciseId+answer);
       System.out.println(result.get("state"));
       if(result.get("state")!="success"){
           return  BaseResponse.fail(result);
       }
       return BaseResponse.success(result);
    }

    /*修改作答结果*/
    @PostMapping(value = "/modifyStuAnswer")
    private BaseResponse<Map<String,Object>> modifyStuAnswer(@RequestParam(value = "studentId") String studentId,
                                            @RequestParam(value = "exerciseId") int exerciseId,
                                            @RequestParam(value = "questionId") int questionId,
                                            @RequestParam(value = "answer") String answer,
                                            @RequestParam(value = "isRight") boolean isRight,
                                            @RequestParam(value = "idea") String idea,
                                            @RequestParam(value = "score") int score)throws ParseException{
        Map<String,Object> result=stuAnswerService.modifyStuAnswer(studentId, exerciseId, questionId, answer, isRight, idea, score);
        System.out.println(222);
        if(result.get("state")!="success"){
            return  BaseResponse.fail(result);
        }
        return BaseResponse.success(result);
    }

    /*修改作答成绩*/
    @PostMapping(value = "/modifyStuScore")
    private BaseResponse<Map<String,Object>> modifyStuScore(@RequestParam(value = "studentId") String studentId,
                                               @RequestParam(value = "exerciseId") int exerciseId,
                                               @RequestParam(value = "questionId") int questionId,
                                               @RequestParam(value = "score") int score)throws ParseException{

        Map<String,Object> result=stuAnswerService.modifyStuScore(studentId, exerciseId, questionId, score);
        System.out.println(111);
        if(result.get("state")!="success"){
            return  BaseResponse.fail(result);
        }
        return BaseResponse.success(result);
    }
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
    @PostMapping(value = "/modifyStuScores")
    private BaseResponse<Map<String,Object>> modifyStuScores(@RequestParam(value = "studentId") String studentId,
                                                             @RequestParam(value = "exerciseId") int exerciseId,
                                                             @RequestParam(value = "questionId") int questionId,
                                                             @RequestParam(value = "answer") String answer,
                                                             @RequestParam(value = "stuanswer") String stuanswer,
                                                             @RequestParam(value = "stuidea") String stuidea,
                                                             @RequestParam(value = "oldscore") int oldscore,
                                                             @RequestParam(value = "newscore") int newscore,
                                                             @RequestParam(value = "studentName") String studentName,
                                                             @RequestParam(value = "stuclass") String stuclass,
                                                             @RequestParam(value = "difficulty") String difficulty,
                                                             @RequestParam(value = "opid") String opid,
                                                             @RequestParam(value = "opname") String opname,
                                                             @RequestParam(value = "optime") Date optime)throws ParseException{


        Map<String,Object> result=stuAnswerService.modifyStuScore(studentId, exerciseId, questionId, newscore);
        if(result.get("state")!="success"){
            return  BaseResponse.fail(result);
        }
        System.out.println(1111);
        //能修改成功了
        Map<String,Object> result1= updateLogService.addupdatelog(studentName,studentId,stuclass, exerciseId,questionId,answer,stuanswer,stuidea,oldscore,newscore, difficulty,opid,opname,optime);
        if(result1.get("state")!="success"){
            return  BaseResponse.fail(result1);
        }
        return BaseResponse.success(result1);
    }




    /*查询某学生某道题作答结果*/
    @GetMapping(value = "/getStuAnswer")
    private BaseResponse<Map<String, Object>> getStuAnswer(@RequestParam(value = "studentId") String studentId,
                                              @RequestParam(value = "exerciseId") int exerciseId,
                                              @RequestParam(value = "questionId") int questionId
                                             )throws ParseException{
        return BaseResponse.success(stuAnswerService.getStuAnswer(studentId, exerciseId, questionId));
    }

    /*查询某班级全部学生在某套试卷上作答结果*/
    @GetMapping(value = "/getClassExerScore")
    private BaseResponse<List<Map<String,Object>>> getClassExerScore(@RequestParam(value = "classId") String classId,
                                                       @RequestParam(value = "exerciseId") int exerciseId
                                                        )throws ParseException{
        List<Map<String,Object>> results=stuAnswerService.getClassExerScore(classId, exerciseId);
        if(results.size()==0){
            return BaseResponse.fail();
        }
        return BaseResponse.success(results);
    }

    /*查询某班级在某套试卷上每一题的平均分*/
    @GetMapping(value = "/getClassExerAve")
    public  BaseResponse<List<Map<String,Object>>> getClassExerAve(@RequestParam(value = "classId") String classId,
                                                    @RequestParam(value = "exerciseId") int exerciseId)throws ParseException{
        List<Map<String,Object>> results=stuAnswerService.getClassExerAve(classId,exerciseId);
        if(results.size()==0){
            return BaseResponse.fail();
        }
        return BaseResponse.success(results);
    }

    /*查询某班级在某套试卷上某一题的得分*/
    @GetMapping(value = "/getClassQuestScore")
    private BaseResponse<List<Map<String,Object>>> getClassQuestScore(@RequestParam(value = "classId") String classId,
                                                        @RequestParam(value = "exerciseId") int exerciseId,
                                                        @RequestParam(value = "questionId") int questionId)throws ParseException{
        List<Map<String,Object>> results=stuAnswerService.getClassQuestScore(classId, exerciseId, questionId);
        if(results.size()==0){
            return BaseResponse.fail();
        }
        return BaseResponse.success(results);
    }
    
    /*显示某学生在某套试卷上的作答结果*/
    @GetMapping(value = "/getStuTheExerAnswer")
    private BaseResponse<List<Map<String,Object>>> getStuTheExerAnswer(@RequestParam(value = "studentId") String studentId,
                                                         @RequestParam(value = "exerciseId") int exerciseId)throws  ParseException{
        System.out.println("-------------------来了1------------------");
        System.out.println(studentId+"   "+exerciseId);
    	List<Map<String,Object>> results=stuAnswerService.getStuTheExerAnswer(studentId, exerciseId);

        if(results.size()==0){
            return BaseResponse.fail();
        }
        return BaseResponse.success(results);
    }

    /*显示某学生在每套试卷上的作答结果*/
    @GetMapping(value = "/getStuAllExerAnswer")
    private BaseResponse<List<Map<String,Object>>> getStuAllExerAnswer(@RequestParam(value = "studentId") String studentId)throws ParseException{
        System.out.println("-------------------来了2------------------");
        List<Map<String,Object>> results=stuAnswerService.getStuAllExerAnswer(studentId);
        if(results.size()==0){
            return BaseResponse.fail();
        }
        return BaseResponse.success(results);
    }
    @GetMapping(value = "/getStuAllExer")
    private BaseResponse<List<Map<String,Object>>> getStuAllExer(@RequestParam(value = "studentId") String studentId)throws ParseException{
        List<Map<String,Object>> results=stuAnswerService.getStuAllExer(studentId);
        if(results.size()==0){
            return BaseResponse.fail();
        }
        return BaseResponse.success(results);
    }
}
